//
//  BulletInputView.swift
//  SkyApp
//
//  Created by 白翊 on 2021/12/20.
//

import UIKit

protocol BulletInputViewProtocol: NSObjectProtocol {
    func updateBullet()
}

class BulletInputView: UIView {

    let textFieldHeight: CGFloat = 40
    
    weak var delegate: BulletInputViewProtocol?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .light.withAlphaComponent(0.5)
        
        addSubview(textField)
        textField.snp.makeConstraints { make in
            make.center.equalTo(self)
            make.height.equalTo(textFieldHeight)
            make.left.equalTo(64)
            make.right.equalTo(-64)
        }
        
        addSubview(playBtn)
        
        let settingBtn: UIButton = {
            $0.setImage(UIImage(named: "icon_setting"), for: .normal)
            $0.addTarget(self, action: #selector(clickSettingAction), for: .touchUpInside)
            return $0
        }(UIButton(type: .custom))
        addSubview(settingBtn)
        
        playBtn.snp.makeConstraints { make in
            make.right.equalTo(textField.snp.left).offset(-10)
            make.width.height.equalTo(28)
            make.centerY.equalTo(textField)
        }
        
        settingBtn.snp.makeConstraints { make in
            make.left.equalTo(textField.snp.right).offset(10)
            make.width.height.equalTo(playBtn)
            make.centerY.equalTo(textField)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func clickPlayOrStopAction() {
        playBtn.isSelected = !playBtn.isSelected
        
        bulletStop = playBtn.isSelected
        delegate?.updateBullet()
    }
    
    @objc private func clickSettingAction() {
        BulletSettingView.showInWindow(delegate: self)
    }
    
    lazy var textField: UITextField = {
        $0.backgroundColor = .light.withAlphaComponent(0.5)
        $0.tintColor = .theme
        $0.leftView = UIView(frame: CGRect(x: 0, y: 0, width: interval, height: 0))
        $0.leftViewMode = .always
        $0.placeholder = "请输入弹幕"
        $0.attributedPlaceholder = NSAttributedString(string: "请输入弹幕", attributes: [.foregroundColor : UIColor.remark, .font : UIFont.systemFont(ofSize: 16)])
        $0.setCornerRadius(radius: textFieldHeight * 0.5)
        $0.font = .systemFont(ofSize: 16)
        $0.textColor = .title
        $0.delegate = self
        return $0
    }(UITextField())
    
    lazy var playBtn: UIButton = {
        $0.setImage(UIImage(named: "icon_stop"), for: .normal)
        $0.setImage(UIImage(named: "icon_play"), for: .selected)
        $0.addTarget(self, action: #selector(clickPlayOrStopAction), for: .touchUpInside)
        return $0
    }(UIButton(type: .custom))
    
}

extension BulletInputView: BulletSettingViewProtocol {
    func updateBullet() {
        delegate?.updateBullet()
    }
}

extension BulletInputView: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        let desc = textField.text
        if let desc = desc, desc.count > 0 {
            bulletDesc = desc
            delegate?.updateBullet()
        }
    }
}
